package com.packt.animation.funky;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;




public class FunkyActivity extends Activity {
	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		final ImageView animImage = (ImageView) findViewById(R.id.stickmanView);
		final AnimationDrawable stickmanAnim = (AnimationDrawable) animImage.getDrawable();
		
		final AnimationDrawable jumpAnim = subAnimation( stickmanAnim, 24, 35 );
		Drawable standingStill = (Drawable) getResources().getDrawable(R.drawable.stickman_frame_00);
		jumpAnim.addFrame(standingStill, 83);
		jumpAnim.setOneShot(true);
		
		animImage.post( 
			new Runnable() {
				public void run() { 
					stickmanAnim.start();
				}
			}
		);
		
		Button danceLeftButton = (Button) findViewById(R.id.danceleft);
		danceLeftButton.setOnClickListener(
			new View.OnClickListener() {
				public void onClick(View v) {
					AnimationDrawable danceLeftAnim = (AnimationDrawable) getResources().getDrawable(R.anim.dance_left);
					animImage.setImageDrawable(danceLeftAnim);
					danceLeftAnim.start();
				}
			}
		);

		Button danceRightButton = (Button)findViewById(R.id.danceright);
		danceRightButton.setOnClickListener(
			new View.OnClickListener() {
				public void onClick(View v) {
					AnimationDrawable danceRightAnim = (AnimationDrawable) getResources().getDrawable(R.anim.dance_right);
					animImage.setImageDrawable(danceRightAnim);
					danceRightAnim.start();
				}
			}
		);

		Button jumpButton = (Button) findViewById(R.id.jump);
		jumpButton.setOnClickListener(
			new View.OnClickListener() {
				public void onClick(View v) {
//					final AnimationDrawable jumpXAnim = subAnimation( animDrawable, 24, 35 );
/*					
					AnimationDrawable jumpXAnim = (AnimationDrawable) getResources().getDrawable(R.anim.jump);
					animImage.setImageDrawable(jumpXAnim);
					Drawable standingStill = (Drawable) getResources().getDrawable(R.drawable.stickman_frame_00);
					jumpXAnim.addFrame(standingStill, 83);
					jumpXAnim.start();
*/					
					
					animImage.setImageDrawable(jumpAnim);
					jumpAnim.start();
					jumpAnim.setVisible(true,true);
					
				}
			}
		);
		
		Button keepOnDancingButton = (Button) findViewById(R.id.keep);
		 keepOnDancingButton.setOnClickListener(
			new View.OnClickListener() {
				public void onClick(View v) {
					AnimationDrawable stickmanAnim = (AnimationDrawable) getResources().getDrawable(R.anim.stickman);
					animImage.setImageDrawable(stickmanAnim);
					stickmanAnim.start();
				}
			}
		);
		 
		Button restButton = (Button) findViewById(R.id.rest);
		restButton.setOnClickListener(
			new View.OnClickListener() {
				public void onClick(View v) {
					AnimationDrawable animDrawable;
					animDrawable = (AnimationDrawable)animImage.getDrawable();
					animDrawable.stop();
				}
			}
		);
		
		Button runButton = (Button) findViewById(R.id.run);
		runButton.setOnClickListener(
			new View.OnClickListener() {
				public void onClick(View v) {
					AnimationDrawable animDrawable;
					animDrawable = (AnimationDrawable)animImage.getDrawable();
					animDrawable.start();
				}
			}
		);
	}

	private AnimationDrawable subAnimation( AnimationDrawable src, int start, int end ) {
		AnimationDrawable subAnim = new AnimationDrawable();
		subAnim.setOneShot( src.isOneShot() );
		for (int i = start; i<=end; ++i) {
			subAnim.addFrame( src.getFrame (i), src.getDuration (i) );
		}
		return subAnim;
	}
}
